package com.easy.sk.controller;

import com.alibaba.fastjson.JSON;
import com.easy.common.service.SKService;
import com.easy.common.util.Result;
import com.easy.common.util.SKRequest;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.Hashtable;
import java.util.UUID;

@RestController
@RequestMapping("sk")
public class SKController {
    @Autowired
    SKService skService;
    /**
     *   /sk/33/12       33就是秒杀活动的id值    12就是skgoodsid
     *
     * @param activityid   秒杀活动   秒杀活动的id
     * @param skgoodsid   秒杀数据   秒杀商品的id
     * @param request     请求对象 获取token  获取userid
     * @return
     */
    @PostMapping("/{activityid}/{skgoodsid}")
    public Result sk(@PathVariable("activityid") Integer activityid,@PathVariable("skgoodsid") Integer skgoodsid, HttpServletRequest request){
        //封装SKRequest对象
        //获取登录用户的id值
        request.getSession().invalidate();
        new File("").exists();
        Hashtable<String,String> map = new Hashtable<>();
        map.put("name","zhangsan");
        Integer userid= 1;  //getUserid(request);
        String skid= UUID.randomUUID().toString();//可以自定义算法生成秒记录id
        SKRequest skRequest = new SKRequest(userid,skgoodsid,activityid,skid);
        //fastJSON将SKRequest转换成JSON字符串，将消息发送到MQ 交给service发送
        String message= JSON.toJSONString(skRequest);
        skService.sendMSG(message);
        //响应请求
        return Result.success(skid);
    }
}
